#include "StdAfx.h"
#include "OfflineData.h"
#include "utils/encryptfile.h"
#include "tinyxml/tinyxml.h"

COfflineData::COfflineData(void)
{
}

COfflineData::~COfflineData(void)
{

}

bool  COfflineData::WriteOfflineData(CString path)
{

	TiXmlDocument doc;  
	TiXmlPrinter   printer;
	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0","gb2312", "no");
	doc.LinkEndChild( decl ); 

	{//Config
		TiXmlElement * Config = new TiXmlElement("Config"); 
		doc.LinkEndChild( Config );  

		{//MC
			TiXmlElement * MC= new TiXmlElement( "MC" );  
			Config->LinkEndChild(MC);
			MC->LinkEndChild(new TiXmlText( m_machineId.GetBuffer()));
		}
		
		{//Serv
			TiXmlElement * Serv= new TiXmlElement( "Serv" );  
			Config->LinkEndChild(Serv);
			{//IP
				TiXmlElement * IP= new TiXmlElement( "IP" );  
				Serv->LinkEndChild(IP);
				IP->LinkEndChild(new TiXmlText( m_timeIp.GetBuffer()));
			}
			{//Port
				TiXmlElement * Port= new TiXmlElement( "Port" );  
				Serv->LinkEndChild(Port);
				Port->LinkEndChild(new TiXmlText( m_timePort.GetBuffer()));
			}
	
		}
		{//Ploy
			TiXmlElement * Ploy= new TiXmlElement( "Ploy" );  
			Config->LinkEndChild(Ploy);

			{//Rule
				TiXmlElement * Rule= new TiXmlElement( "Rule" );  
				TiXmlText*  text   = new  TiXmlText(m_rule.GetBuffer());
				text->SetCDATA(true);
				Ploy->LinkEndChild(Rule);
				Rule->LinkEndChild(text);
			}
			{//Key
				TiXmlElement * Key= new TiXmlElement( "Key" );  
				Ploy->LinkEndChild(Key);
				Key->LinkEndChild(new TiXmlText( m_fileKey.GetBuffer()));
			}
		}
		{//TimeEnd
			TiXmlElement * TimeEnd= new TiXmlElement( "TimeEnd" );  
			Config->LinkEndChild(TimeEnd);
			TimeEnd->LinkEndChild(new TiXmlText( m_timeEnd.GetBuffer()));

		}
	}

	
	doc.Accept(&printer);

	CEncryptFile    enFIle;
	TRACE0(printer.CStr());
	if(!enFIle.Save(printer.CStr(),printer.Size(),path,m_machineId))
	{
		return false;
	}
	return true;


}